package com.yonyou.gmmc.service.JDInterface.controller;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.enmus.ExcelType;
import org.marketing.common.constants.ResultMsgConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.infoservice.filestore.FileStore;
import org.marketing.common.Response;
import com.yonyou.gmmc.service.JDInterface.constant.JDInterfaceConstant;
import com.yonyou.gmmc.service.JDInterface.model.TtJdOrderVarianc;
import com.yonyou.gmmc.service.JDInterface.service.JDInterfaceService;
import com.yonyou.gmmc.service.JDInterface.service.ProductService;

import io.swagger.annotations.ApiOperation;

/**
 * @author Administrator TODO description
 * @date 2017年3月8日
 */
@RestController
@RequestMapping(value = JDInterfaceConstant.BASE_PATH + "/" + JDInterfaceConstant.VERSION)
public class ProductController {
	private Logger logger = Logger.getLogger(ProductController.class);
	@Autowired
	private ProductService productService;
	@Autowired
	private JDInterfaceService jdInterfaceService;

	/**
	 * 获取商品池编号
	 * 
	 * @author lyq
	 * @date 2017年4月11日
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getPageNum", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "获取商品池编号", notes = "获取商品池编号")
	public Response<Map<String, Object>> getPageNum() {
		logger.info("ProductController.getPageNum==>start...");
		try {
			String token = jdInterfaceService.getToken();
			logger.info("ProductController.getPageNum==>token值：" + token);
			Map<String, Object> map = productService.getPageNum(token);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("获取商品池编号异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("ProductController.getPageNum==>end...");
		}
	}

	/**
	 * 获取商品池商品
	 * 
	 * @author lyq
	 * @date 2017年4月11日
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getSku", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "获取商品池商品", notes = "获取商品池商品")
	public Response<Map<String, Object>> getSku() {
		logger.info("ProductController.getSku==>start...");
		try {
			Map<String, Object> map = productService.getSku();
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("获取商品池商品异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("ProductController.getSku==>end...");
		}
	}

	/**
	 * 获取商品上下架状态
	 * 
	 * @author lyq
	 * @date 2017年4月17日
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/skuState", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "获取商品上下架状态", notes = "获取商品上下架状态")
	public Response<Map<String, Object>> skuState() {
		logger.info("ProductController.skuState==>start...");
		try {
			Map<String, Object> map = productService.skuState();
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("获取商品上下架状态异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("ProductController.skuState==>end...");
		}
	}

	/**
	 * 获取商品京东价格
	 * 
	 * @author lyq
	 * @date 2017年4月17日
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getJdPrice", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "获取商品京东价格", notes = "获取商品京东价格")
	public Response<Map<String, Object>> getJdPrice() {
		logger.info("ProductController.getJdPrice==>start...");
		try {
			Map<String, Object> map = productService.getJdPrice();
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("获取商品京东价格异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("ProductController.getJdPrice==>end...");
		}
	}

	/**
	 * 获取商品协议价格
	 * 
	 * @author lyq
	 * @date 2017年4月17日
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getPrice", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "获取商品协议价格", notes = "获取商品协议价格")
	public Response<Map<String, Object>> getPrice() {
		logger.info("ProductController.getPrice==>start...");
		try {
			Map<String, Object> map = productService.getPrice();
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("获取商品协议价格异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("ProductController.getPrice==>end...");
		}
	}

	/**
	 * 获取商品分类信息
	 * 
	 * @author lyq
	 * @date 2017年4月17日
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getCategory", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "获取商品分类信息", notes = "获取商品分类信息")
	public Response<Map<String, Object>> getCategory() {
		logger.info("ProductController.getCategory==>start...");
		try {
			String token = jdInterfaceService.getToken();
			Map<String, String> params = new HashMap<>();
			params.put("token", token);
			Map<String, Object> map = productService.getCategory(params);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("获取商品分类信息异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("ProductController.getCategory==>end...");
		}
	}

	/**
	 * 获取商品池内商品组
	 * 
	 * @author liyujiao
	 * @date 2017年4月17日
	 * @param skuId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getSimilarSku", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "获取商品池内商品组", notes = "获取商品池内商品组")
	public Response<Map<String, Object>> getSimilarSku() {
		logger.info("ProductController.getSimilarSku==>start...");
		try {
			Map<String, Object> map = productService.getSimilarSku();
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("获取商品池内商品组异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("ProductController.getSimilarSku==>end...");
		}
	}

	/**
	 * 新建订单查询接口
	 * 
	 * @author liyujiao
	 * @date 2017年4月17日
	 * @param date
	 *            必传(2017-04-17) 不含当天
	 * @param page
	 *            当前页码 默认1
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getNewOrder", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "新建订单查询", notes = "新建订单查询")
	public Response<Map<String, Object>> getNewOrder() {
		logger.info("ProductController.getNewOrder==>start...");
		try {
			productService.getEnterpriseMarketing("new");
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, "");
		} catch (Exception e) {
			logger.error("新建订单异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("ProductController.getNewOrder==>end...");
		}

	}
	/**
     * PC差异订单查询
     * @author lyq
     * @date 2017年8月20日
     * @return
     * @throws Exception
     */
	@RequestMapping(value = "/variancOrder", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "差异订单查询", notes = "差异订单查询")
	public Response<PageInfo<TtJdOrderVarianc>> variancOrder(
			@RequestParam(required=false,defaultValue="1") Integer pageNum,
     		@RequestParam(required=false,defaultValue="JD_ORDER_ID") String sort,
     		@RequestParam(required=false,defaultValue="asc") String sortType,
     		@RequestParam(required=false,defaultValue="10") String pageSize,
			@RequestParam(required=false,defaultValue="") String orderId,//订单号
			@RequestParam(required=false,defaultValue="") Integer variancType//差异类型 0两边存在订单  1京东存在  2商城存在
			//,@RequestParam(required=false,defaultValue="") String date
			) {//订单日期
		logger.info("ProductController.variancOrder==>start...");
		try {
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("pageNum", pageNum);
			paramMap.put("sort", sort+"  "+ sortType);
			paramMap.put("pageSize", pageSize);
			paramMap.put("orderId", "%"+orderId+"%");
			paramMap.put("variancType", variancType);
			//paramMap.put("date", date);
			PageInfo<TtJdOrderVarianc> page = productService.variancOrder(paramMap);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS,page, "");
		} catch (Exception e) {
			logger.error("新建订单异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("ProductController.variancOrder==>end...");
		}

	}
	/**
     * PC差异订单导出
     * @author lyq
     * @date 2017年8月20日
     * @return
	 * @throws IOException 
     * @throws Exception
     */
	@RequestMapping(value = "/variancOrderExport", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "差异订单查询", notes = "差异订单查询")
	public Response<Map<String,String>> variancOrderExport(
			@RequestParam(required=false,defaultValue="1") Integer pageNum,
     		@RequestParam(required=false,defaultValue="JD_ORDER_ID") String sort,
     		@RequestParam(required=false,defaultValue="asc") String sortType,
     		@RequestParam(required=false,defaultValue="10") String pageSize,
			@RequestParam(required=false,defaultValue="") String orderId,//订单号
			@RequestParam(required=false,defaultValue="") Integer variancType//差异类型
			//,@RequestParam(required=false,defaultValue="") String date
			){//订单日期
		logger.info("ProductController.variancOrder==>start...");
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		try {
			Map<String, Object> paramMap = new HashMap<>();
//			paramMap.put("pageNum", pageNum);
//			paramMap.put("sort", sort);
			paramMap.put("sortType", sortType);
			paramMap.put("pageSize", pageSize);
			paramMap.put("orderId", "%"+orderId+"%");
			paramMap.put("variancType", variancType);
			//paramMap.put("date", date);
			List<TtJdOrderVarianc> list = productService.variancOrderExport(paramMap);
			ExportParams params = new ExportParams("订单列表","订单信息",ExcelType.XSSF);
	        Workbook book = ExcelExportUtil.exportExcel(params, TtJdOrderVarianc.class, list);
	        book.write(os);  
	        String fileName = FileStore.getInstance().write("nd02", "fs01", "variancList.xlsx", os.toByteArray());
	          
	        Map<String,String> fileResult = new HashMap<>();
	        fileResult.put("fileName","variancList.xlsx" );
	        fileResult.put("url", FileStore.getInstance().getDomainURL(fileName));
	     	   
	     	return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, fileResult, "");
	    } catch (Exception e) {
            logger.error("查询状态发生异常",e);
            return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
	    } finally {
			try {
				os.close();
			} catch (IOException e) {
				logger.info("ProductController.variancOrder==>IO异常："+e);
			}
			logger.info("ProductController.variancOrder==>end...");
		}

	}

	/**
	 * 获取妥投订单接口
	 * 
	 * @author liyujiao
	 * @date 2017年4月17日
	 * @param date
	 *            必传(2017-04-17) 不含当天
	 * @param page
	 *            当前页码 默认1
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getDlokOrder", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "获取妥投订单", notes = "获取妥投订单")
	public Response<Map<String, Object>> getDlokOrder() {
		logger.info("ProductController.getDlokOrder==>start...");
		try {
			productService.getEnterpriseMarketing("dlok");
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, "");
		} catch (Exception e) {
			logger.error("获取妥投订单异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("ProductController.getDlokOrder==>end...");
		}

	}

	/**
	 * 获取拒收消息接口
	 * 
	 * @author liyujiao
	 * @date 2017年4月17日
	 * @param date 必传(2017-04-17) 不含当天
	 * @param page 当前页码 默认1
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getRefuseOrder", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "获取拒收消息", notes = "获取拒收消息")
	public Response<Map<String, Object>> getRefuseOrder() {
		logger.info("ProductController.getRefuseOrder==>start...");
		try {
			productService.getEnterpriseMarketing("refuse");
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, "");
		} catch (Exception e) {
			logger.error("获取拒收消息接异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("ProductController.getRefuseOrder==>end...");
		}
	}

	/**
	 * 查询分类列表信息接口
	 * 
	 * @author Administrator TODO description
	 * @date 2017年4月14日
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({"rawtypes" })
	@RequestMapping(value = "/insertCategorys", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "保存分类列表信息接口", notes = "保存分类列表信息接口")
	public Response insertCategorys() {
		logger.info("ProductController.insertCategorys==>start...");
		try {
			String token = jdInterfaceService.getToken();
			logger.info("==>token值：" + token);
			productService.checkCategorys(token);
			return new Response(ResultMsgConstants.RESULT_CODE_SUCCESS, "");
		} catch (Exception e) {
			logger.error("保存分类列表信息", e);
			return new Response(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("ProductController.insertCategorys==>end...");
		}

	}

	/**
	 * 商品可售验证接口
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/saleState", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "获取商品可售状态", notes = "获取商品可售状态")
	public Response<Map<String, Object>> saleState() {
		logger.info("ProductController.saleState==>start...");
		try {
			Map<String, Object> map = productService.saleState();
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("获取商品可售状态异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("ProductController.saleState==>end...");
		}
	}

	/**
	 * 商品售卖价接口
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/sellPrice", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "商品售卖价", notes = "商品售卖价")
	public Response<Map<String, Object>> sellPrice() {
		logger.info("ProductController.sellPrice==>start...");
		try {
			Map<String, Object> map = productService.sellPrice();
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("查询商品售卖价异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("ProductController.sellPrice==>end...");
		}
	}
	
	/**
	 * 批量上架商品接口
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/productIns", method = RequestMethod.POST)
	@ApiOperation(value = "批量上架商品", notes = "批量上架商品")
	public Response<Map<String, Object>> productIns(@RequestBody Map<String,Object> param) {
		logger.info("ProductController.productIns==>start...");
		try {
		    List<String> list = (List<String>) param.get("sku");
			Map<String, Object> map = productService.productIns(list);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("查询商品售卖价异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("ProductController.productIns==>end...");
		}
	}
}
